package com.aqie.libfactory.data.helper;


import com.aqie.libfactory.model.db.Session;
import com.aqie.libfactory.model.db.Session_Table;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.sql.language.Where;

import java.util.List;

/**
 * 会话辅助工具类
 */
public class SessionHelper {
    // 从本地查询Session
    public static Session findFromLocal(String id) {
        return SQLite.select()
                .from(Session.class)
                .where(Session_Table.id.eq(id))
                .querySingle();
    }

    /**
     * 更新指定 session未读消息数
     * @param id
     * @return
     */
    public static void updateUnReadNumber(String id) {
        Where<Session> where = SQLite.update(Session.class)
                .set(Session_Table.unReadCount.eq(0L))
                .where(Session_Table.id.eq(id));
        where.execute();
    }

    /**
     * 更新全部session 未读消息数为0
     * @return
     */
    public static void updateAllUnReadNumber() {
        Where<Session> update = SQLite.update(Session.class)
                .set(Session_Table.unReadCount.eq(0L))
                .where(Session_Table.unReadCount.greaterThan(0L));
        update.execute();
    }

    public static List<Session> searchAllSession(){
        return SQLite.select()
                .from(Session.class)
                .orderBy(Session_Table.modifyAt, false) // false 是倒序
                .limit(1000)
                .queryList();
    }
}
